<?php
namespace User\Controller;

use Think\Controller;

class IndexController extends Controller
{
    private $uid;
    private $open_id;
    //初始化
    public function _initialize()
    {
        $uid=cookie('user_userId');
        $open_id=cookie('user_openid');
        if($uid && $open_id){
            $this->uid=$uid;
            $this->open_id=$open_id;
        }else{
            $this->error('数据失效,重新授权',U('Accredit/bind'));
        }

    }

    //员工中心
    public function index()
    {
        $userinfo=D('Member')->userDetail($this->uid);
        $in_time=strtotime(date('Y-m-d'));
        $lore=M('Location')->field("lid,label,sr_time")->where("gl_time=%d and uid=%d",[$in_time,$this->uid])->select();
        $this->assign('lore',$lore);
        $this->assign('userinfo',$userinfo);
        $this->assign('title','个人中心');
        $this->display();
    }
    //记录拜访结果
    public function callFollow()
    {

        if(IS_POST){
           $_POST['wechat_id']=$this->open_id;
           $_POST['sr_time']=time();
           $_POST['gl_time']=strtotime(date('Y-m-d'));
           $arr=D('Call')->addLog($_POST);
           if($arr['state']=='success'){
               $this->success($arr['info'],U('Index/logList'));
           } else{
               $this->error($arr['info']);
           }

        }else{
         $this->assign('uid',$_GET['uid']);
         $this->assign('title','记录拜访结果');
         $this->display();
        }
    }
    //拜访记录列表
    public function logList($uid='',$page=0)
    {

           if($uid==''){
               $uid=$this->uid;
           }
        $re=D('Call')->lister($uid,$page);
        $this->assign('re',$re);
        $this->assign('title','拜访记录');
        $this->display();
    }

    //记录详情
    public function logDetail($cid='')
    {

        $re=M('Call')->where("cid=%d",$cid)->find();
        $this->assign('re',$re);
        $this->assign('title','拜访详情');
        $this->display();
    }
    //更多数据
    public function dataMore(){
        if(IS_POST){
            $page=$_POST['page'];
            if($page>6){
                $page=$page-1;
            }
            $re=D('Call')->lister($this->uid,$page);
            if($re){
                $arr['state']='success';
                $arr['info']=$re;
                $this->ajaxReturn($arr);
            }else{
                $arr['state']='error';
                $arr['info']='没有更多数据';
                $this->ajaxReturn($arr);
            }

        }
    }



}